#include "DownSample.h"

/////////////////////////////////////
// CDownSample
/////////////////////////////////////
CDownSample::CDownSample()
{
	mr_DownSampledTexture_=NULL;
	mr_Surface_[0]=mr_Surface_[1]=NULL;
	mr_Texture_[0]=mr_Texture_[1]=NULL;
	mi_Width=0;
	mi_Height=0;
	mk_Mask_=NULL;
	mr_BlurHShader_=mr_BlurVShader_=mr_BeforeBlurShader_=NULL;
	mi_Steps=0;
	mb_Use=false;
}


CDownSample::~CDownSample()
{
	if(mr_BlurHShader_) delete mr_BlurHShader_;
	if(mr_BlurVShader_) delete mr_BlurVShader_;
	if(mr_BeforeBlurShader_) delete mr_BeforeBlurShader_;
	if(mk_Mask_) delete mk_Mask_;
	SAFE_RELEASE(mr_Surface_[0]);
	SAFE_RELEASE(mr_Surface_[1]);
	SAFE_RELEASE(mr_Texture_[0]);
	SAFE_RELEASE(mr_Texture_[1]);
}


void CDownSample::Create(IDirect3DDevice9 *inDevice,int inmi_Width, int inmi_Height, int inmi_Steps, IShader *h, IShader *v, IShader *a)
{
	//mb_Use=true;
	//mr_Device_=inDevice;
	//mi_Width=inmi_Width;
	//mi_Height=inmi_Height;

	//float delta = float(mi_Width)/mi_Height+(1-float(mi_Height)/mi_Width);
	//POWEROF2(mi_Width);
	//POWEROF2(mi_Height);

	//mr_BlurHShader_=h;
	//mr_BlurVShader_=v;
	//mr_BeforeBlurShader_=a;
	//mi_Steps=inmi_Steps;

	//double ddx = (1.0f/mi_Width);
	//double ddy = (1.0f/mi_Height)*delta;

	//mr_BlurHShader_->VM.SetDouble("sddx",ddx);
	//mr_BlurHShader_->VM.SetDouble("sddy",ddy);

	//mr_BlurVShader_->VM.SetDouble("sddx",ddx);
	//mr_BlurVShader_->VM.SetDouble("sddy",ddy);

	//if(mr_BeforeBlurShader_)
	//{
	//	mr_BeforeBlurShader_->VM.SetDouble("sddx",ddx);
	//	mr_BeforeBlurShader_->VM.SetDouble("sddy",ddy);
	//}

	//mk_Mask_ = new ScreenMask();
	//float bias=0.5f;
	//mk_Mask_->Create(mr_Device_,-bias,-bias,mi_Width-bias,mi_Height-bias,true);

	//IDirect3D9 *d3d;
	//mr_Device_->GetDirect3D(&d3d);
	//D3DDISPLAYMODE d3ddisplaymode;
	//d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddisplaymode);

	//if(D3D_OK!=mr_Device_->CreateTexture(mi_Width,mi_Height,1,D3DUSAGE_RENDERTARGET,D3DFMT_A8R8G8B8,//d3ddisplaymode.Format,
	//						D3DPOOL_DEFAULT,
	//						&mr_Texture_[0],
	//						NULL))
	//						PrintLog("BloomTonemap: DownSamplemr_Texture_ 1 create error\n");

	//if(D3D_OK!=mr_Device_->CreateTexture(mi_Width,mi_Height,1,D3DUSAGE_RENDERTARGET,D3DFMT_A8R8G8B8,//d3ddisplaymode.Format,
	//						D3DPOOL_DEFAULT,
	//						&mr_Texture_[1],
	//						NULL))
	//						PrintLog("BloomTonemap: DownSamplemr_Texture_ 2 create error\n");

}


LPDIRECT3DTEXTURE9 CDownSample::GetDownSampledTexture()
{
	return mr_DownSampledTexture_;
}


LPDIRECT3DTEXTURE9 CDownSample::SampleDown(LPDIRECT3DTEXTURE9 tex)
{
	/*SAFE_RELEASE(mr_Surface_[0]);
	SAFE_RELEASE(mr_Surface_[1]);
	mr_Texture_[0]->Getmr_Surface_Level(0, &mr_Surface_[0]);
	mr_Texture_[1]->Getmr_Surface_Level(0, &mr_Surface_[1]);
	LPDIRECT3DTEXTURE9 blur=tex;

	if(mi_Steps%2!=0)
		mi_Steps++;

	if(mr_BeforeBlurShader_)
	{
		CBloomTonemap::SetRenderTarget(0,mr_Surface_[1]);
		mr_Device_->BeginScene();
		mk_Mask_->SetTexture(blur);
		mk_Mask_->Render(mr_Device_,mr_BeforeBlurShader_);
		mr_Device_->EndScene();
		blur=mr_Texture_[1];
	}
	

	for(int i=0; i<mi_Steps; i++)
	{
		if(i==0)
			mk_Mask_->SetTexture(blur);
		if(i%2==0)
		{
			blur=mr_Texture_[1];
			if(i!=0)
				mk_Mask_->SetTexture(blur);
			CBloomTonemap::SetRenderTarget(0,mr_Surface_[0]);
		}
		else
		{
			blur=mr_Texture_[0];
			if(i!=0)
				mk_Mask_->SetTexture(blur);
			CBloomTonemap::SetRenderTarget(0,mr_Surface_[1]);		
		}
		mr_Device_->BeginScene();
		
		if(i%2==0)
			mk_Mask_->Render(mr_Device_,mr_BlurVShader_);
		else
			mk_Mask_->Render(mr_Device_,mr_BlurHShader_);
		mr_Device_->EndScene();
	}
	mr_DownSampledTexture_=mr_Texture_[0];
	return mr_DownSampledTexture_;*/
	return NULL;
}
